package com.sky.service.impl;

import com.sky.dto.GoodsSalesDTO;
import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.ReportService;
import com.sky.vo.OrderReportVO;
import com.sky.vo.SalesTop10ReportVO;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserReportVO;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ReportServiceImpl implements ReportService {
    @Autowired
private OrderMapper orderMapper;
    @Autowired
private UserMapper userMapper;
    /**
     * 营业额统计
     * @param begin
     * @param end
     * @return
     */
    @Override
    public TurnoverReportVO getTurnoverStatistics(LocalDate begin, LocalDate end) {
        //当前集合用于存放从begin到end范围内每天的日期
        List<LocalDate> dateList = new ArrayList<>();

    dateList.add(begin);
   while (!begin.equals(end)){
       //日期计算，计算指定日期的后一天对应的日期
        begin = begin.plusDays(1);
        dateList.add(begin);}
   //将集合转化为数组同时，以逗号分隔


        List<Double> turnoverList = new ArrayList<>();
    //营业额的统计算
        for (LocalDate localDate : dateList) {
            //查询日期对应的营业额：状态为已完成订单金额合计
            LocalDateTime beginTime = LocalDateTime.of(localDate, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(localDate, LocalTime.MAX);
            HashMap<Object, Object> map = new HashMap<>();
            map.put("begin",beginTime);
            map.put("end",endTime);
            map.put("stats",Orders.COMPLETED);
            Double turnover = orderMapper.sumByMap(map);
            turnover= turnover == null ? 0.0 : turnover;
            turnoverList.add(turnover);
            //select sum(amount) from orders where order_time  > beginTimeand order order_time < ? and status = ?(5)
        }
        //封装饭返回结果
        String date = StringUtils.join(dateList, ",");
        String turn = StringUtils.join(turnoverList, ",");
        TurnoverReportVO turnoverReportVO = TurnoverReportVO.builder()
                .dateList(date)
                .turnoverList(turn)
                .build();
        return turnoverReportVO;
    }

    @Override
    public UserReportVO getUserStatistics(LocalDate begin, LocalDate end) {
        //当前集合用于存放从begin到end范围内每天的日期
        List<LocalDate> dateList = new ArrayList<>();
        dateList.add(begin);
        while (!begin.equals(end)){
            //日期计算，计算指定日期的后一天对应的日期
            begin = begin.plusDays(1);
            dateList.add(begin);}
        //将日期转化为数组并以逗号分隔
        String date = StringUtils.join(dateList, ",");
        //存放每天用户新增数量
        ArrayList<Integer> newUserList = new ArrayList<>();
        //每天的总用户数量
        ArrayList<Integer> totalUserList = new ArrayList<>();
//每天用户总量
        for (LocalDate localDate : dateList) {
            //查询日期对应的营业额：状态为已完成订单金额合计
            LocalDateTime beginTime = LocalDateTime.of(localDate, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(localDate, LocalTime.MAX);
            HashMap<Object, Object> map = new HashMap<>();

            map.put("end",endTime);
            //统计用户总数
            Integer totalUser = userMapper.countByMap(map);
            totalUserList.add(totalUser);
            map.put("begin",beginTime);
            //统计新增用户
            Integer newUser = userMapper.countByMap(map);
            newUserList.add(newUser);
        }
        String newUser = StringUtils.join(newUserList, ",");
        String totalUser = StringUtils.join(totalUserList, ",");
        UserReportVO userReportVO = UserReportVO.builder()
                .dateList(date)
                .newUserList(newUser)
                .totalUserList(totalUser).build();
        return userReportVO;
    }

    @Override
    public OrderReportVO getOrdersStatistics(LocalDate begin, LocalDate end) {
        //存放从begin到end每天的日期
        ArrayList<LocalDate> dateList = new ArrayList<>();
        dateList.add(begin);
        //将区间内时间全部封装到集合
        while (!begin.equals(end)){
            begin = begin.plusDays(1);
            dateList.add(begin);
        }
        //将时间列表转换为数组
        String date = StringUtils.join(dateList, ",");
        HashMap<Object, Object> map = new HashMap<>();
        //每日订单数列表
        ArrayList<Integer> orderCountList = new ArrayList<>();
        //每日有效订单数列表
        ArrayList<Integer> validOrderCountList = new ArrayList<>();
        //订单总数
        Integer totalOrderCount = 0;
        //有效订单总数
        Integer validTotalOrderCount = 0;
        //计算每天日期的范围,每天都需要计算，用for循环
        for (LocalDate localDate : dateList) {
            //对应日期的开头
            LocalDateTime beginTime = LocalDateTime.of(localDate, LocalTime.MIN);
            //对应日期的结束
            LocalDateTime endTime = LocalDateTime.of(localDate, LocalTime.MAX);
            map.put("begin",beginTime);
            map.put("end",endTime);
            map.put("status",null);
            //每日订单数 select count(id) from orders where
            Integer orderCount = orderMapper.countByMap(map);
            orderCountList.add(orderCount);
            //计算订单总数
            totalOrderCount=totalOrderCount+orderCount;
            //每日有效订单书
            map.put("status",Orders.COMPLETED);
            Integer validOrderCount = orderMapper.countByMap(map);
            validOrderCountList.add(validOrderCount);
            //计算有效订单总数
            validTotalOrderCount = validTotalOrderCount + validOrderCount;
        }
        String order = StringUtils.join(orderCountList, ",");
        String validOrder = StringUtils.join(validOrderCountList, ",");
//订单完成率
        Double orderCompletionRate=0.0;
        if (totalOrderCount!=0){
            orderCompletionRate = (double) (validTotalOrderCount/totalOrderCount);
        }
        OrderReportVO orderReportVO = OrderReportVO.builder()
                .dateList(date)
                .orderCountList(order)
                .validOrderCountList(validOrder)
                .totalOrderCount(totalOrderCount)
                .validOrderCount(validTotalOrderCount)
                .orderCompletionRate(orderCompletionRate)
                .build();



return orderReportVO;
    }



    /**
     * 根据条件统计订单数量
     * @param begin
     * @param end
     * @param status
     * @return
     */
    private Integer getOrderCount(LocalDateTime begin,LocalDateTime end,Integer status){
        HashMap<Object, Object> map = new HashMap<>();
        map.put("begin",begin);
        map.put("end",end);
        map.put("status",status);
        return orderMapper.countByMap(map);
    }

    /**
     * 销量排名前十统计
     * @param begin
     * @param end
     * @return
     */
    @Override
    public SalesTop10ReportVO getSalesTop10(LocalDate begin, LocalDate end) {
        //当前日期开始时间
        LocalDateTime beginTime = LocalDateTime.of(begin, LocalTime.MIN);
        //当前日期结束时间
        LocalDateTime endTime = LocalDateTime.of(end, LocalTime.MAX);
        List<GoodsSalesDTO> salesTop10List = orderMapper.getSalesTop(beginTime,endTime);
        List<Integer> numberlist = salesTop10List.stream().map(GoodsSalesDTO::getNumber).collect(Collectors.toList());
        List<String> namelist = salesTop10List.stream().map(GoodsSalesDTO::getName).collect(Collectors.toList());
        String names = StringUtils.join(namelist, ",");
        String numbers = StringUtils.join(numberlist, ",");
        SalesTop10ReportVO salesTop10ReportVO = SalesTop10ReportVO.builder()
                .nameList(names)
                .numberList(numbers)
                .build();

        return salesTop10ReportVO;
    }




}
